package com.cheer.dto;

import com.cheer.entity.Applicant;
import com.cheer.entity.Organization;
import com.cheer.entity.StandingBook;
import com.cheer.entity.StandingBookDocument;
import com.cheer.enums.DocTypeEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@Accessors(chain = true)
public class StandingBookSaveDTO {

    @NotBlank(message = "集体负责人不能为空")
    @Length(max = 20, message = "集体负责人不能超过20个字符")
    private String contactPerson;

    @NotBlank(message = "集体负责人电话不能为空")
    @Length(max = 20, message = "集体负责人电话不能超过20个字符")
    private String contactPersonPhone;

    @NotBlank(message = "集体负责人职务不能为空")
    @Length(max = 200, message = "集体负责人职务不能超过200个字符")
    private String contactPersonPost;

    @NotNull(message = "集体负责人出生日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date contactPersonBirth;

    @NotNull(message = "职工人数不能为空")
    @Range(min = 6, max = 200, message = "职工人数的范围为 6-200 人")
    private Integer personNumber;

    @NotNull(message = "35周岁以下青年人数不能为空")
    private Integer youthNumber;

    @Length(max = 20, message = "集体电话不能超过20个字符")
    private String phone;

    @NotBlank(message = "地址不能为空")
    @Length(max = 200, message = "地址不能超过200个字符")
    private String address;

    @Length(max = 2000, message = "简要事迹不能超过2000个字符")
    private String achievement;

    @Length(max = 2000, message = "近三年获奖情况不能超过2000个字符")
    private String award;

    private Integer reviewType;

    private Long joinReviewOrgId;

    @NotBlank(message = "成员基本情况文档不能为空")
    private String memberDocUrl;

    private List<String> fileDocUrls;


    public Applicant buildApplicant(Long applicantId, Organization joinOrg) {
        Applicant applicant = new Applicant();
        applicant.setId(applicantId);
        applicant.setReviewType(reviewType);
        if (joinOrg != null) {
            applicant.setJoinReviewOrgId(joinOrg.getId());
            applicant.setJoinReviewOrgPath(joinOrg.getOrgPath());
        }
        applicant.setJoinReviewOrgId(joinReviewOrgId);
        applicant.setContactPerson(contactPerson);
        applicant.setContactPersonPhone(contactPersonPhone);
        applicant.setContactPersonPost(contactPersonPost);
        applicant.setContactPersonBirth(contactPersonBirth);
        applicant.setPersonNumber(personNumber);
        applicant.setYouthNumber(youthNumber);
        applicant.setPhone(phone);
        applicant.setAddress(address);
        return applicant;
    }

    public StandingBook buildStandingBook(Long accountId) {
        StandingBook standingBook = buildUpdateStandingBook(null);
        standingBook.setAccountId(accountId);
        return standingBook;
    }

    public StandingBook buildUpdateStandingBook(Long standingBookId) {
        StandingBook standingBook = new StandingBook();
        standingBook.setId(standingBookId);
        if (StringUtils.isBlank(achievement)) {
            standingBook.setAchievement(StringUtils.EMPTY);
        } else {
            standingBook.setAchievement(achievement);
        }
        if (StringUtils.isBlank(award)) {
            standingBook.setAward(StringUtils.EMPTY);
        } else {
            standingBook.setAward(award);
        }
        return standingBook;
    }

    public List<StandingBookDocument> buildDocuments(Long accountId) {
        List<StandingBookDocument> documents = new ArrayList<>();
        StandingBookDocument memberDoc = new StandingBookDocument();
        memberDoc.setAccountId(accountId);
        memberDoc.setDocType(DocTypeEnum.MEMBER.getCode());
        memberDoc.setDocUrl(memberDocUrl);
        documents.add(memberDoc);
        if (fileDocUrls != null) {
            for (String fileDocUrl : fileDocUrls) {
                StandingBookDocument fileDoc = new StandingBookDocument();
                fileDoc.setAccountId(accountId);
                fileDoc.setDocType(DocTypeEnum.FILE.getCode());
                fileDoc.setDocUrl(fileDocUrl);
                documents.add(fileDoc);
            }
        }
        return documents;
    }
}
